<template>
  <div style="height:100%" v-loading="loading">
    <v-tableTitle
      :btnConfig="btnConfig"
      :title="$t('transmit.importfield')"
      @newCreate="newCreate"
      @copy="copy"
      @del="del"
    ></v-tableTitle>
    <GTable ref="table" :hasCheck="true" :hasEdit="true" api="excelImportField/selectExcelImportFieldList" :otherHeight="0"  :columnConfig="columnConfig" @selection-change="handleSelectionChange"
      @setdialog="setdialog">
    </GTable>
  </div>
</template>

<script>
import baseMixin from "@/mixins/BaseMixin";
import agGridBaseMixin from "@/mixins/AgGridBaseMixin";
import {selectExcelImportFieldList, deleteExcelImportFieldByIds, getExcelImportType} from "@/api/transmit/transmit";

export default {
  name: "excelImportField",
  mixins: [baseMixin, agGridBaseMixin],
  data() {
    return {
      height: 0,
      columnConfig:{ menu_id: 957, menu_code:"excelImportField", menu_list_code:"excelImportField" },
      excelImportTypes: [],
      defaultColDef: {}
    };
  },
  computed: {
    btnConfig() {
      return [
        { label: this.$t('common.create'), certigier: this.newStatus||(!this.btnPermNameList.includes('EN02070101')&&!this.btnPermNameList.includes('WM03160101')), invalid: "newCreate" },
        { label: this.$t('common.copy'), certigier: this.copyStatus||(!this.btnPermNameList.includes('EN02070102')&&!this.btnPermNameList.includes('WM03160102')), invalid: "copy" },
        { label: this.$t('common.delete'), certigier: this.delStatus||(!this.btnPermNameList.includes('EN02070103')&&!this.btnPermNameList.includes('WM03160103')), invalid: "del" }
      ];
    }
  },
  mounted() {
    this.getExcelImportType();
    this.height = this.getHeight(window,60);
    this.$store.commit("setSkip", {});
    this.$store.commit("setRouteId", "");
    this.$store.commit("setNewFlag", false);
    this.$store.commit("setRouteData", {});
    this.getTableData(this.page, this.pageSize);
  },
  methods: {
    getExcelImportType() {
      getExcelImportType().then(res => {
        this.excelImportTypes = res;
      });
    },
    newCreate() {
      this.$store.commit("setNewFlag", true);
      this.$router.push({name: "excelImportFieldDrawer"});
    },
    copy() {
      let selectedList = this.gridObj.getSelectedRows();
      if (selectedList.length !== 1 || selectedList === null || selectedList === undefined) {
        this.$message.error(this.$t('msg.msg11'));
        return;
      }

      this.$store.commit("setNewFlag", true);
      // 子页面传值
      let query = {copy: true, obj:selectedList[0]};
      this.$router.push({name: "excelImportFieldDrawer", query: query});
    },
    del() {
      let selectedList = this.gridObj.getSelectedRows();
      if (selectedList.length === 0 || selectedList === null || selectedList === undefined) {
        this.$message.error(this.$t('msg.msg12'));
        return;
      }
      let data = [];
      selectedList.forEach(item => {
        data.push(item.id);
      });
      deleteExcelImportFieldByIds(data).then(res => {
          if (res.data === -1) {
            let text = "要删除Excel导入字段定义信息，请先删除对应导入模板配置信息后重试！!";
            this.$message.error(text);
          } else {
            this.$message.success(this.$t('app.delMes'));
            this.getTableData(this.page, this.pageSize);
          }
        
      });
    },
    agGridReady(params) {
      this.gridObj = params.api;
    },
    setdialog(data) {
      this.$store.commit("setRouteData", data);
      this.$router.push({name: "excelImportFieldDrawer"});
    },
    getTableData(page, pageSize) {
      console.log("getTableData【page = %o， pageSize = %o】", page, pageSize);
      let data = { pageNum: page, pageSize: pageSize };
      selectExcelImportFieldList(data).then(res => {
          this.rowData = res.data.list;

          // 转义-导入类型
          this.rowData.forEach((item, index) => {
            // console.log("列表转义 = %o，对象信息 = %o", index, item);
            let importType = item.importType;
            this.excelImportTypes.forEach((x) => {
              let code = parseInt(x.code);
              // console.log("a = %o, b = %o", importType, code);
              if (importType === code) {
                item.importType = x.description;
              }
            });
          });
          this.page = res.pageNum;
          this.pageSize = res.pageSize;
          this.total = res.data.total;
        
      });
    }
  }
};
</script>

<style></style>
